import com.mysql.jdbc.MysqlDataTruncation;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBCSelect{
    public static void main(String[] args) throws SQLException {
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSl=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        //2,建立连接
        Connection connection=dataSource.getConnection();

        //3,构造sql
        String sql="select * from student";

        PreparedStatement statement=connection.prepareStatement(sql);
        //4.上传到服务器
        ResultSet resultSet=statement.executeQuery();
        //result就是结果结合,代表了查询返回的临时表
        while (resultSet.next()){
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            System.out.println("id: "+id);
            System.out.println("name: "+name);
        }
        //5,释放资源
        resultSet.close();
        statement.close();
        connection.close();

    }
}
